/**
 * Copyright 2021 xpstem.com
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.xpstem.lelink.operation;

import com.xpstem.lelink.enums.OpCode;
import com.xpstem.lelink.type.Data8;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

/**
 * EV3 operation package.
 * @author billy zhang
 */
public class EV3OperationPackage {

    private int gvSize = 0;
    private short lvSize = 0;
    private OpCode opCode;
    /**
     * 操作请求数据
     */
    private byte[] opData;

    public EV3OperationPackage(OpCode opCode, byte[] opData ) {
        this( opCode,  opData, 0 );
    }

    public EV3OperationPackage( OpCode opCode, byte[] opData, int gvSize ) {
        this.opCode = opCode;
        this.opData = opData;
        this.gvSize = gvSize;
    }

    public int getGvSize() {
        return gvSize;
    }

    public short getLvSize() {
        return lvSize;
    }

    public OpCode getCode() { return opCode; }

    public byte[] getBytes() throws IOException {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

        Data8.from(opCode.value()).writeTo(outputStream);
        if (opData != null) {
            outputStream.write(opData);
        }
        return outputStream.toByteArray();
    }

}
